/*
 * Class: StateDAO
 * Created by: Mary Joy Pascual
 * Date Created: 06/16/2014
 * Company: Netsuite
*/
package com.netsuite.dao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPException;

import com.netsuite.adapter.StateAdapter;
import com.netsuite.constants.Constants;
import com.netsuite.helper.ErrorHelper;
import com.netsuite.helper.ErrorSOAPHelper;
import com.netsuite.logger.LoggerImp;
import com.netsuite.pojo.StatePOJO;
import com.netsuite.webservices.lists.accounting_2014_1.State;
import com.netsuite.webservices.platform.core_2014_1.GetAllRecord;
import com.netsuite.webservices.platform.core_2014_1.GetAllResult;
import com.netsuite.webservices.platform.core_2014_1.Record;
import com.netsuite.webservices.platform.core_2014_1.RecordList;
import com.netsuite.webservices.platform.core_2014_1.RecordRef;
import com.netsuite.webservices.platform.core_2014_1.types.GetAllRecordType;
import com.netsuite.webservices.platform.faults_2014_1.ExceededRecordCountFault;
import com.netsuite.webservices.platform.faults_2014_1.ExceededRequestLimitFault;
import com.netsuite.webservices.platform.faults_2014_1.ExceededRequestSizeFault;
import com.netsuite.webservices.platform.faults_2014_1.ExceededUsageLimitFault;
import com.netsuite.webservices.platform.faults_2014_1.InvalidCredentialsFault;
import com.netsuite.webservices.platform.faults_2014_1.InvalidSessionFault;
import com.netsuite.webservices.platform.messages_2014_1.WriteResponse;
import com.netsuite.webservices.platform_2014_1.NetSuitePortType;

public class StateDAO {
	
	private static String CLASS_NAME = StateDAO.class.getName();
	
	private NetSuitePortType _service = null;

	public StateDAO() throws ErrorHelper {
		try {
			MainDAO mainDAO = new MainDAO();
			_service = mainDAO.get_service();
		} catch (FileNotFoundException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_FNF+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_FNF+ e.getMessage());
		} catch (ServiceException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SE+ e.getMessage());
		} catch (IOException e) {
			LoggerImp.errorLog(CLASS_NAME,Constants.ERROR_IO+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_IO+ e.getMessage());
		} catch (SOAPException e) {
			LoggerImp.errorLog(CLASS_NAME,Constants.ERROR_SOAP+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SOAP+ e.getMessage());
		}

	}
	
	public List<StatePOJO> getAllStates() throws ErrorHelper{

		LoggerImp.startLog(CLASS_NAME, "getAllStates()");
		
		List<StatePOJO> stateList = new ArrayList<StatePOJO>();
	
		GetAllRecord stateRef = new GetAllRecord();
		stateRef.setRecordType(GetAllRecordType.state);
	
		try {
			GetAllResult readResponse = _service.getAll(stateRef);
			if(readResponse.getStatus().isIsSuccess()){
				RecordList recList = readResponse.getRecordList();
				for (Record record: recList.getRecord()){
					State state = (State) record;
					stateList.add(StateAdapter.setState(state));
				}
				LoggerImp.infoLog(CLASS_NAME, "Load Successful: No. of Records Generated = "+stateList.size());
			} 
		} catch (ExceededRequestSizeFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "getAllStates()");
		
		return stateList;
		
	}
	
	public String addState(StatePOJO statePojo) throws ErrorHelper {
		
		LoggerImp.startLog(CLASS_NAME, "addState()");
		
		String internalId = "";
		State state = StateAdapter.setState(statePojo);
		try {
			LoggerImp.debugLog(CLASS_NAME, "Adding State Record....");
			WriteResponse writeResponse = _service.add(state);
			if(!writeResponse.getStatus().isIsSuccess()){
				ErrorSOAPHelper.displayError(CLASS_NAME, writeResponse.getStatus().getStatusDetail());
	  		} else {
	  			RecordRef rec = (RecordRef)writeResponse.getBaseRef();
	  			internalId = rec.getInternalId();
	  			LoggerImp.infoLog(CLASS_NAME, "State Id#"+internalId+" successfully generated");
	  		}
		} catch (ExceededRequestSizeFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "addState()");
		
  		return internalId;
	}

	public String updateState(StatePOJO statePojo) throws ErrorHelper {
		
		LoggerImp.startLog(CLASS_NAME, "updateState()");

		State state = StateAdapter.setState(statePojo);
		try {
			LoggerImp.debugLog(CLASS_NAME, "Updating State Record....");
			WriteResponse writeResponse = _service.update(state);
			if(!writeResponse.getStatus().isIsSuccess()){
				ErrorSOAPHelper.displayError(CLASS_NAME, writeResponse.getStatus().getStatusDetail());
	  		} else {
	  			LoggerImp.infoLog(CLASS_NAME, "State Id#"+state.getInternalId()+" was successfully updated");
	  		}
			
		} catch (ExceededRequestSizeFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SIZ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SIZ+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_INV+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_INV+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_USE+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_USE+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REQ+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_REQ+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_MAX+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_MAX+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SES+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_REM+ e.getMessage());
			throw new ErrorHelper(CLASS_NAME,Constants.ERROR_SES+ e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "updateCustomer()");
	
		return "";
	}
}
